package com.huo.thread.waitnotifydemo;

public class WaitNotifyDemo {

    private volatile int value;

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public static void main(String[] args) {
        WaitNotifyDemo d = new WaitNotifyDemo();




        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (d){
                    if (d.getValue() != 1){
                        try {
                            System.out.println("等待");
                            d.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("执行完毕");
                }

            }
        }).start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (d) {
                    System.out.println("修改状态的线程执行...");
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    d.setValue(1);
                    System.out.println("状态值修改成功。。。");
                    d.notify();
                }

            }
        }).start();

    }


}
